<?php
	if( !defined('IN') )
	define('IN', TRUE);

	require_once 'common.php';
	
	$tabdata = $enchersObj->getEnchereUne();
	if(!empty($tabdata)){
		foreach ($tabdata as $enchers) {
		   $pc=$enchers['limite']==0?'b':'';
		   if( !isset($enchers['ae']) || $enchers['ae'] == 0 ) $ae = 'b';
				$enchers['ae'] = 'AE';
		   if(!empty($enchers['images'])){
			   $enchers['images'] = array_shift(explode(';', $enchers['images']));
		   }
           $enchers['enchere_g'] = (float)$enchers['enchere']>(float)( $enchers['prix']*$enchers['limite']/100)?' enchere_g':'';
		   $enchers['enchere'] = str_replace('.', ',', $enchers['enchere']);
           
           list($datee, $heuree) = explode(' ', $enchers['dateFin']);
           $datee = explode('-', $datee);
           $heuree = explode(':', $heuree);
           $datef = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0]));
           
		   $time_en = (intval( $enchers['begin'])==0?$enchers['duree']:$datef - time());
		   $timeEn = $time_en > 0?$tools->afficherDate($time_en):_TERMINE_;
		   //gestion du temps
		   $val = $enchers['retime'];
		   $enchers['retime'] =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
		   $enchers['pseudo'] = (($enchers['pseudo']!=null)?$enchers['pseudo']:'Aucun');
		   if(intval($enchers['begin'])==0){
			   $enchersObj->updateBeginEnchere(array( 'id'=>$enchers['id'],'dateFin'=>date('Y-m-d H:i:s',time()+intval($enchers['duree']))));
		   }	
		
	 ?>
		<div id="une">
			<div class="iconsu">
				<div class="iconu">
					<span class="rt"><?php echo $enchers['retime']; ?></span>
				</div>
				<div class="iconu">
					<span class="pc<?php echo $pc  ?>"><?php  echo $enchers['limite']; ?>%</span>
				</div>
				<div class="iconu">
					<span class="ae<?php echo $ae  ?>"><?php  echo $enchers['ae']; ?></span>
				</div>
			</div>
			<div class="titreu"><?php  echo $enchers['nom']; ?></div>
			<div id="effect<?php  echo $enchers['id']; ?>">
				<a href="detail-product-<?php  echo $enchers['id']; ?>.html">
					<div class="imageu"><img src="<?php  echo _UPLOAD_DIR_ ?>encheres/meduim<?php  echo $enchers['images']?>" alt="<?php  echo $enchers['nom']; ?>" title="<?php  echo $enchers['nom']; ?>" /></div>
				</a>
			</div>
			<div class="temps">
				<?php if( $site->getHourClose() <= date('H:i:s') OR $site->getHourOpen() >= date('H:i:s')): ?>
					EN PAUSE
				<?php else: ?>
					<?php echo $timeEn ?>
				<?php endif; ?>
			</div>
			<div class="enchere<?php echo $enchers['enchere_g']; ?>"><?php  echo $enchers['enchere']; ?> <?php  echo $site->getDevise(); ?></div>
			<div class="prixu">
				<?php echo _PRIX_MAGASIN_ ?><?php  echo $enchers['prix']; ?> <?php  echo $site->getDevise(); ?><br />
				<span class="meneuru">
					<?php if($timeEn == _TERMINE_): ?>
						<?php echo _GAGNANT_ ?>
					<?php else: ?>
						<?php echo _MENEUR_; ?>
					<?php endif; ?>
				</span>
				<span class="pseudou"><?php  echo $enchers['pseudo']; ?></span>
			</div>
			<div class="encheriru">
				<?php if( $site->getHourClose() <= date('H:i:s') OR $site->getHourOpen() >= date('H:i:s')): ?>
					<img src="<?php  echo _THEME_IMG_DIR_ ?>bt-avenir.jpg" alt="Indisponible" />
				<?php else: ?>
					<?php if($timeEn == _TERMINE_): ?>
						<img src="<?php  echo _THEME_IMG_DIR_ ?>bt-termine.jpg" alt="Indisponible" />
					<?php elseif($user->isAuthenticated() AND $enchers['meneur'] == $user->getId()): ?>
						<img src="<?php  echo _THEME_IMG_DIR_ ?>bt-meneur.jpg" alt="Meneur" />
					<?php else: ?>
						<a class="encherir-elt" href="#" id="encherir-<?php  echo $enchers['id'].'-'.($user->isAuthenticated()?intval($user->getId()):0); ?>"><img src="<?php  echo _THEME_IMG_DIR_ ?>encherir.jpg" alt="Ench&eacute;rir" /></a>
					<?php endif; ?>
				<?php endif; ?>
			</div>
		</div>
	<?php }
	}else echo '<div id="une"><p>'._AUCUNE_ENCHERE_UNE_.'</p></div>';
	
	$tabdata = $enchersObj->getEnchereEnCours();
	
	if(!empty($tabdata)){
		foreach ($tabdata as $data) {
			$data['pc']=$data['limite']==0?'b':'';
		   if( !isset($data['ae']) || $data['ae'] == 0 ) $data['ae'] = 'b';
				$data['AE'] = 'AE';
		   if(!empty($data['images'])){
			   $data['images'] = array_shift(explode(';', $data['images']));
		   }
           $data['enchere_g'] = (float)$data['enchere']>(float)( $data['prix']*$data['limite']/100)?' enchere_g':'';
		   $data['enchere'] = str_replace('.', ',', $data['enchere']);
           
           list($datee, $heuree) = explode(' ', $data['dateFin']);
           $datee = explode('-', $datee);
           $heuree = explode(':', $heuree);
           $datef = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0]));
           
		   $time_en = (intval( $data['begin'])==0?$data['duree']:$datef - time());
		   $data['timeEn'] = $time_en > 0?$tools->afficherDate($time_en):_TERMINE_; 
		   $val = $data['retime'];
		   $data['retime'] =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
		   $data['pseudo'] = (($data['pseudo']!=null)?$data['pseudo']:'Aucun');
		   if(intval($data['begin'])==0){
			   $enchersObj->updateBeginEnchere(array( 'id'=>$data['id'],'dateFin'=>date('Y-m-d H:i:s',time()+intval($data['duree']))));
		   }
		 ?>
			<div class="cours">
				<div class="icons">
					<div class="icon">
						<span class="rt"><?php  echo $data['retime']; ?></span>
					</div>
					<div class="icon">
						<span class="pc<?php  echo $data['pc']; ?>"><?php  echo $data['limite']; ?>%</span>
					</div>
					<div class="icon">
						<span class="ae<?php  echo $data['ae']; ?>"><?php  echo $data['AE']; ?></span>
					</div>
				</div>
				<div class="content-img">
					<div class="titre"><?php  echo $data['nom']; ?></div>
					<div id="effect<?php  echo $data['id']; ?>">
						<div class="image">
							<a href="detail-product-<?php  echo $data['id']; ?>.html">
								<img src="<?php  echo _UPLOAD_DIR_ ?>encheres/small<?php  echo $data['images']; ?>" alt="<?php  echo $data['nom']; ?>" title="<?php  echo $data['nom']; ?>"/>
							</a>
						</div>
					</div>
				</div>
				<div class="temps">
				<?php if( $site->getHourClose() <= date('H:i:s') OR $site->getHourOpen() >= date('H:i:s')): ?>
					EN PAUSE
				<?php else: ?>
					<?php echo $data['timeEn'] ?>
				<?php endif; ?>
				</div>
				<div class="enchere<?php echo $data['enchere_g']; ?>"><?php  echo $data['enchere']; ?> <?php  echo $site->getDevise(); ?></div>
				<div class="encherir">
				<?php if( $site->getHourClose() <= date('H:i:s') OR $site->getHourOpen() >= date('H:i:s')): ?>
					<img src="<?php  echo _THEME_IMG_DIR_ ?>bt-avenir.jpg" alt="Indisponible" />
				<?php else: ?>
					<?php if($data['timeEn'] == _TERMINE_): ?>
						<img src="<?php  echo _THEME_IMG_DIR_ ?>bt-termine.jpg" alt="Indisponible" />
					<?php elseif($user->isAuthenticated() AND $data['meneur'] == $user->getId()): ?>
						<img src="<?php  echo _THEME_IMG_DIR_ ?>bt-meneur.jpg" alt="Meneur" />
					<?php else: ?>
						<a class="encherir-elt" href="#" id="encherir-<?php  echo $data['id'].'-'.($user->isAuthenticated()?intval($user->getId()):0); ?>"><img src="<?php  echo _THEME_IMG_DIR_ ?>encherir.jpg" alt="Ench&eacute;rir" /></a>
					<?php endif; ?>
				<?php endif; ?>
				</div>
				<div class="infos">
					<span class="prix"><?php echo _PRIX_MAGASIN_ ?><?php  echo $data['prix']; ?> <?php  echo $site->getDevise(); ?></span>
					<span class="meneur">
					<?php if($data['timeEn'] == _TERMINE_): ?>
						<?php echo _GAGNANT_ ?>
					<?php else: ?>
						<?php echo _MENEUR_; ?>
					<?php endif; ?>
					</span>
					<span class="pseudo"><?php  echo $data['pseudo']; ?></span>
				</div>
			</div>
		 <?php }
	}else echo '<div class="cours"><p>'._AUCUNE_ENCHERE_EN_COURS_.'</p></div>';	?>
<script type="text/javascript">
// <![CDATA[
( function($) { 
    $(document).ready(function(){
        $('.encherir-elt').unbind('load').click(function(){
            var idelt = this.id;
            idelt = idelt.split('-');
            iduser =idelt.pop();
            nbre_credits = $('#nber_cdt').html();
            nbre_credits = Number(nbre_credits);
            if(iduser==0 || nbre_credits==0){
                alert("<?php echo _INFOS_ENCHERIR_ ?>");
                return false;
            }else{
                idpro =idelt.pop();
                $.ajax({
                    type: "POST", 
                    url: "traitemntajax.html",
                    data: "iduser="+iduser+"&idprod="+idpro+"&cred="+nbre_credits,
                    cache: false,
                    success:  function(response){
                        $('#nber_cdt').html(nbre_credits-1);
                    },

                    onFailure: function(xhr){
                        alert("Erreur lors du placement de l'ench&egrave;re");
                    },
                })
            }
            return false;
        });
    });
} ) ( jQuery )
//]]>
</script>